import { HashRouter,useRoutes,Navigate, useLocation } from "react-router-dom"
import { NotFound } from "./views/error/404"
import { Suspense , lazy} from 'react'
import { Loading } from "./views/loading"
import { Homes } from "./views/home/homes"
import { NotServer } from "./views/error/503"
import { Guanggao } from "./views/error/guangao"
import { MySearch } from "./views/search/search"
import { MyScan } from "./views/scan/MyScan"
import { MyPhoto } from "./views/photo/photo"
import {useEffect} from 'react'
import { useLocalStorageState } from "ahooks"


const Logins = lazy(()=>import('./views/login/login'))
const Admin = lazy(()=>import('./views/home/childs/admin'))
const Fenlei = lazy(()=>import("./views/home/childs/fenlei"))
const Conts = lazy(()=>import("./views/home/childs/Cons"))
const MyWd = lazy(()=>import("./views/home/childs/MyWd"))
const MyReg = lazy(()=>import("./views/login/reg"))
const FindPass = lazy(()=>import("./views/login/findPass"))
const SetPass = lazy(()=>import("./views/home/childs/setPass"))
const City = lazy(()=>import("./views/home/childs/city"))
const AddMs = lazy(()=>import("./views/home/childs/addMS"))
const WdMs = lazy(()=>import("./views/home/childs/wdms"))
const Datail = lazy(()=>import("./views/home/childs/datail"))
const MYLikes = lazy(()=>import("./views/home/childs/myLikes"))
const MYPings = lazy(()=>import("./views/home/childs/myPings"))
export const RouterData = ()=>{
    
    return(
            <HashRouter>
                <Suspense fallback={<Loading/>}>
                <RouterViews/>
                </Suspense>
            </HashRouter>
    )
}

const RouterViews = ()=>{
    const {pathname} = useLocation();
    const [fromPath,setFromPath] = useLocalStorageState<string|undefined>(
        'frompath',
    )
    const [toPath,setToPath] = useLocalStorageState<string|undefined>(
        'topath',
    )
    console.log(fromPath,toPath);
    
    useEffect(()=>{
        setFromPath(toPath);
        setToPath(pathname)
    },[pathname])
    return useRoutes([
        {
            path:'/404',
            element:<NotFound/>
        },
        {
            path:'/login',
            element:<Logins/>
        },
        {
            path:'/reg',
            element:<MyReg/>
        },
        {
            path:'/findpass',
            element:<FindPass/>
        },
        {
            path:'/',
            element:<Navigate to='/503'/>
        },

        {
            path:'/homes',
            element:<Homes/>,
            children:[
                {
                    path:'/homes',
                    element:<Navigate to='/homes/admin'/>
                },
                {
                    path:'admin',
                    element:<Admin/>
                },
                {
                    path:'fenlei',
                    element:<Fenlei/>
                },
                {
                    path:'conts',
                    element:<Conts/>
                },
                {
                    path:'my',
                    element:<MyWd/>
                },
                
            ]
        },
        {
            path:'/503',
            element:<NotServer/>
        },
        {
            path:'/city',
            element:<City/>
        },
        {
            path:'/addms',
            element:<AddMs/>
        },
        {
            path:'/setpass',
            element:<SetPass/>
        },
        {
            path:"/guangao",
            element:<Guanggao/>
        },
        {
            path:'/search',
            element:<MySearch/>
        },
        {
            path:'/loading',
            element:<Loading />
        },
        {
            path:'/scan',
            element:<MyScan/>
        },
        {
            path:'/photo',
            element:<MyPhoto/>
        },
        {
            path:'/wdms',
            element:<WdMs/>
        },
        {
            path:'/detail/:id',
            element:<Datail/>
        },
        {
            path:'/myXQ/:title',
            element:<MYLikes/>
        },
        {
            path:'/mypings',
            element:<MYPings/>
        },
        {
            path:'*',
            element:<Navigate to='/404'/>
        }
    ])
}